package controllers

import java.io.File
import javax.inject.Inject

import play.api.mvc._
import dao._
import utils.Utils

import scala.concurrent.ExecutionContext.Implicits.global

class PaperController @Inject()(cc:ControllerComponents,literatureDao: LiteratureDao) extends AbstractController(cc) {

  def toIndex = Action.async {
    literatureDao.selectAll.map { x =>
      Ok(views.html.paper.index(x))
    }
  }

  def getDetail(id: Int) = Action.async {
    literatureDao.selectById(id).map { x =>
      Ok(views.html.paper.detail(x))
    }
  }

  def viewPaper(id: Int) = Action.async { implicit request =>
    literatureDao.selectById(id).map { x =>
      val file = new File(Utils.paperPath, x.filename + ".pdf")
      Ok.sendFile(file).withHeaders(
        CACHE_CONTROL -> "max-age=3600"
      )
    }


  }


}
